/**
 * Eclipse Outliner
 * Copyright (C) 2006 Jeff Mesnil
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License. 
 */
package net.jmesnil.opml.core.tests;

import junit.framework.TestCase;
import net.jmesnil.opml.core.IBody;
import net.jmesnil.opml.core.IOPML;
import net.jmesnil.opml.core.IOutline;
import net.jmesnil.opml.core.OPMLPlugin;
import net.jmesnil.opml.core.internal.Outline;

public class OutlineInsertAfterTestCase extends TestCase {

    private IBody body;

    protected void setUp() throws Exception {
        super.setUp();
        IOPML opml = OPMLPlugin.getDefault().createOPML();
        body = opml.getBody();
        assertNotNull(body);
    }

    protected void tearDown() throws Exception {
        body = null;
        super.tearDown();
    }

    public void testBodyInsertAfter() throws Exception {
        assertEquals(0, body.getChildren().length);
        IOutline firstOutline = body.appendNewOutline("first");
        IOutline secondOutline = body.appendNewOutline("second");
        assertEquals(2, body.getChildren().length);
        body.insertAfter(firstOutline, secondOutline);
        assertEquals(2, body.getChildren().length);
        assertEquals(secondOutline, body.getChildren()[0]);
        assertEquals(firstOutline, body.getChildren()[1]);
    }

    public void testBodyInsertAfterNull() throws Exception {
        assertEquals(0, body.getChildren().length);
        IOutline firstOutline = body.appendNewOutline("first");
        IOutline secondOutline = body.appendNewOutline("second");
        assertEquals(2, body.getChildren().length);
        IOutline insertedOutline = body.insertAfter(firstOutline, null);
        assertNull(insertedOutline);
        assertEquals(2, body.getChildren().length);
        assertEquals(firstOutline, body.getChildren()[0]);
        assertEquals(secondOutline, body.getChildren()[1]);
    }

    public void testBodyInsertNullAfterOutline() throws Exception {
        assertEquals(0, body.getChildren().length);
        IOutline rootOutline = body.appendNewOutline("root");
        assertEquals(1, body.getChildren().length);
        IOutline insertedOutline = body
                .insertAfter((Outline) null, rootOutline);
        assertNull(insertedOutline);
        assertEquals(1, body.getChildren().length);
        assertEquals(rootOutline, body.getChildren()[0]);
    }

    public void testBodyInsertNullAfterNull() throws Exception {
        assertEquals(0, body.getChildren().length);
        IOutline insertedOutline = body.insertAfter((Outline) null, null);
        assertNull(insertedOutline);
        assertEquals(0, body.getChildren().length);
    }

    public void testOutlineInsertAfter() throws Exception {
        assertEquals(0, body.getChildren().length);
        IOutline rootOutline = body.appendNewOutline("root");
        IOutline firstNestedOutline = rootOutline
                .appendNewOutline("first nested");
        IOutline secondNestedOutline = rootOutline
                .appendNewOutline("second nested");
        rootOutline.insertAfter(firstNestedOutline, secondNestedOutline);
        assertEquals(secondNestedOutline, rootOutline.getChildren()[0]);
        assertEquals(firstNestedOutline, rootOutline.getChildren()[1]);
    }

    public void testOutlineInsertAfterNull() throws Exception {
        assertEquals(0, body.getChildren().length);
        IOutline rootOutline = body.appendNewOutline("root");
        IOutline firstNestedOutline = rootOutline
                .appendNewOutline("first nested");
        IOutline insertedOutline = rootOutline.insertAfter(firstNestedOutline,
                null);
        assertNull(insertedOutline);
        assertEquals(1, rootOutline.getChildren().length);
        assertEquals(firstNestedOutline, rootOutline.getChildren()[0]);
    }

    public void testOutlineInsertNullAfterOutline() throws Exception {
        assertEquals(0, body.getChildren().length);
        IOutline rootOutline = body.appendNewOutline("root");
        IOutline firstNestedOutline = rootOutline
                .appendNewOutline("first nested");
        IOutline insertedOutline = rootOutline.insertAfter((Outline) null,
                firstNestedOutline);
        assertNull(insertedOutline);
        assertEquals(1, rootOutline.getChildren().length);
        assertEquals(firstNestedOutline, rootOutline.getChildren()[0]);
    }

    public void testOutlineInsertNullAfterNull() throws Exception {
        assertEquals(0, body.getChildren().length);
        IOutline rootOutline = body.appendNewOutline("root");
        IOutline insertedOutline = rootOutline
                .insertAfter((Outline) null, null);
        assertNull(insertedOutline);
        assertEquals(0, rootOutline.getChildren().length);
    }
}
